<?php

namespace App\Admin\Controllers\System;

use App\Admin\Repositories\System\Setting;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Tab;
use App\Models\System\Setting as Model;

class SettingController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Setting(), function (Grid $grid) {
            $grid->wrap(function () {
                $tab = Tab::make();
                $tab->add("顶部标题", $this->tab('0'));
                foreach (Model::where('parent_id', 0)->get() as $value) {
                    $tab->add($value->title, $this->tab($value->id));
                }
                return $tab;
            });
        });
    }

    private function tab($id)
    {
        return Grid::make(Model::where('parent_id', $id), function (Grid $grid) {
            $grid->column('id')->sortable()->width('10%');
            $grid->column('title');
            $grid->column('value')->display(function () {
                switch ($this->input_type) {
                    case 'text':
                        $str = $this->value;
                        break;
                    case 'upload':
                        $str = '<img data-action="preview-img" src="' . env('APP_URL') . '/storage/' . $this->value . '" style="max-height:45px;cursor:pointer" class="img img-thumbnail">';
                        break;
                    default:
                        $str = $this->value;
                        break;
                }
                return $str;
            });
            $grid->disableRowSelector();
            $grid->disablePagination();
            $grid->disableRefreshButton();
            $grid->disableFilterButton();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Setting(), function (Show $show) {
            $show->field('id');
//            $show->field('parent_id');
            $show->field('title');
            $show->field('input_type');
            $show->field('value');
//            $show->field('remake');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Setting(), function (Form $form) {
            $form->display('id');
            $form->select('parent_id')->options((new Setting())->select_list());
            $form->text('title');
            switch ($form->model()->input_type) {
                case 'text':
                    $form->text('value');
                    break;
                case 'upload':
                    $form->image('value')->autoUpload()->uniqueName()->required();
                    break;
            }
            if ($form->isCreating()) {
                $form->select('input_type')->options([
                    'text' => '普通字符',
                    'upload' => '图片',
                ]);
            }

            $form->disableResetButton();
            $form->disableViewCheck();
            $form->disableEditingCheck();
            $form->disableCreatingCheck();
        });
    }
}
